// AnnotationDwgManager.ts

import { OpAnnotationDwgCloud } from "./OpAnnotationDwgCloud";
import { OpAnnotationDwgRect } from "./OpAnnotationDwgRect";
import { OpAnnotationDwgCircle } from "./OpAnnotationDwgCircle";
import { OpAnnotationDwgLead } from "./OpAnnotationDwgLead";
import { OpAnnotationDwgText } from "./OpAnnotationDwgText";

/**
 * 标注管理器，设置不同的标注模式
 */
class AnnotationDwgManager {
  app: any;

  opAnnotationDwgCloud: OpAnnotationDwgCloud;
  opAnnotationDwgRect: OpAnnotationDwgRect;
  opAnnotationDwgCircle: OpAnnotationDwgCircle;
  opAnnotationDwgLead: OpAnnotationDwgLead;
  opAnnotationDwgText: OpAnnotationDwgText;

  annotationLineSize: number;
  annotationTextSize: number;
  annotationColor: string;

  constructor(app: any) {
    this.app = app;

    this.opAnnotationDwgCloud = new OpAnnotationDwgCloud(this.app);
    this.opAnnotationDwgRect = new OpAnnotationDwgRect(this.app);
    this.opAnnotationDwgCircle = new OpAnnotationDwgCircle(this.app);
    this.opAnnotationDwgLead = new OpAnnotationDwgLead(this.app);
    this.opAnnotationDwgText = new OpAnnotationDwgText(this.app);

    this.annotationLineSize = 2;
    this.annotationTextSize = 500;
    this.annotationColor = "#ff0000";
  }

  /**
   * 销毁标注管理器
   */
  dispose() {
    this.opAnnotationDwgCloud.dispose();
    this.opAnnotationDwgRect.dispose();
    this.opAnnotationDwgCircle.dispose();
    this.opAnnotationDwgLead.dispose();
    this.opAnnotationDwgText.dispose();

    // @ts-ignore
    this.opAnnotationDwgCloud = null;
    // @ts-ignore
    this.opAnnotationDwgRect = null;
    // @ts-ignore
    this.opAnnotationDwgCircle = null;
    // @ts-ignore
    this.opAnnotationDwgLead = null;
    // @ts-ignore
    this.opAnnotationDwgText = null;
  }

  /**
   * 设置标注模式
   * @param type
   */
  setAnnotationType(type: AnnotationDwgType) {
    this.opAnnotationDwgCloud.disposeEvent();
    this.opAnnotationDwgRect.disposeEvent();
    this.opAnnotationDwgCircle.disposeEvent();
    this.opAnnotationDwgLead.disposeEvent();
    this.opAnnotationDwgText.disposeEvent();

    switch (type) {
      case AnnotationDwgType.CLOUD:
        this.opAnnotationDwgCloud.initEvent();
        break;
      case AnnotationDwgType.RECT:
        this.opAnnotationDwgRect.initEvent();
        break;
      case AnnotationDwgType.CIRCLE:
        this.opAnnotationDwgCircle.initEvent();
        break;
      case AnnotationDwgType.LEAD:
        this.opAnnotationDwgLead.initEvent();
        break;
      case AnnotationDwgType.TEXT:
        this.opAnnotationDwgText.initEvent();
        break;
    }
  }

  /**
   * 绑定引线文字标注引线绘制完成后的回调-执行弹窗输入文字
   * @param callback
   */
  bindLeadFinishCallback(callback: Function) {
    this.opAnnotationDwgLead.bindFinishCallback(callback);
  }

  /**
   * 引线标注的绘制文字接口
   * @param text
   */
  createLeadText(text: string) {
    const pixelScale2d = this.app.bimViewer.mainCamera.getPixelScale2d();
    //@ts-ignore
    this.opAnnotationDwgLead.selectedAnnotation?.createLeadText(
      text,
      pixelScale2d
    );
  }

  /**
   * 文字标注的绘制文字接口
   * @param text
   */
  createText(text: string) {
    const mesh = this.opAnnotationDwgText.createText(text);
    this.opAnnotationDwgText.tempAnnotation = mesh;
    this.opAnnotationDwgText.annotationMeshArr.push(mesh);
    this.opAnnotationDwgText.setSelectAnnotation(null);
  }

  /**
   * 删除选中标注结果
   */
  deleteSelectAnnotation() {
    this.opAnnotationDwgCloud.deleteSelectAnnotation();
    this.opAnnotationDwgRect.deleteSelectAnnotation();
    this.opAnnotationDwgCircle.deleteSelectAnnotation();
    this.opAnnotationDwgLead.deleteSelectAnnotation();
    this.opAnnotationDwgText.deleteSelectAnnotation();
  }

  /**
   * 删除全部标注结果
   */
  deleteAllAnnotation() {
    this.opAnnotationDwgCloud.deleteAllAnnotation();
    this.opAnnotationDwgRect.deleteAllAnnotation();
    this.opAnnotationDwgCircle.deleteAllAnnotation();
    this.opAnnotationDwgLead.deleteAllAnnotation();
    this.opAnnotationDwgText.deleteAllAnnotation();
  }

  /**
   * 设置所有标注结果显隐
   * @param value
   */
  setAnnotationMeshArrVisible(value: boolean) {
    this.opAnnotationDwgCloud.setAnnotationMeshArrVisible(value);
    this.opAnnotationDwgRect.setAnnotationMeshArrVisible(value);
    this.opAnnotationDwgCircle.setAnnotationMeshArrVisible(value);
    this.opAnnotationDwgLead.setAnnotationMeshArrVisible(value);
    this.opAnnotationDwgText.setAnnotationMeshArrVisible(value);
  }

  /**
   * 设置下一个标注线框
   * @param value
   */
  setNextAnnotationLineSize(val: number) {
    if (val < 1) val = 1;
    if (val > 10) val = 10;

    this.annotationLineSize = val;

    this.opAnnotationDwgCloud.setNextLineSize(val);
    this.opAnnotationDwgRect.setNextLineSize(val);
    this.opAnnotationDwgCircle.setNextLineSize(val);
    this.opAnnotationDwgLead.setNextLineSize(val);
    this.opAnnotationDwgText.setNextLineSize(val);
  }

  /**
   * 设置下一个文字大小
   * @param value
   */
  setNextAnnotationTextSize(val: number) {
    this.annotationTextSize = val;

    this.opAnnotationDwgCloud.setNextTextSize(val);
    this.opAnnotationDwgRect.setNextTextSize(val);
    this.opAnnotationDwgCircle.setNextTextSize(val);
    this.opAnnotationDwgLead.setNextTextSize(val);
    this.opAnnotationDwgText.setNextTextSize(val);
  }

  /**
   * 设置下一个标注颜色
   * @param value
   */
  setNextAnnotationColor(color: string) {
    this.annotationColor = color;

    this.opAnnotationDwgCloud.setNextColor(color);
    this.opAnnotationDwgRect.setNextColor(color);
    this.opAnnotationDwgCircle.setNextColor(color);
    this.opAnnotationDwgLead.setNextColor(color);
    this.opAnnotationDwgText.setNextColor(color);
  }

  /**
   * 获取所有标注数据
   */
  getAnnotationAllData() {
    const cloud = this.opAnnotationDwgCloud.getAnnotationData();
    const rect = this.opAnnotationDwgRect.getAnnotationData();
    const circle = this.opAnnotationDwgCircle.getAnnotationData();
    const lead = this.opAnnotationDwgLead.getAnnotationData();
    const text = this.opAnnotationDwgText.getAnnotationData();

    const data = { cloud, rect, circle, lead, text };
    return JSON.stringify(data);
  }

  /**
   * 设置所有标注
   * @param json
   */
  setAnnotationAllData(json: string) {
    this.deleteAllAnnotation();

    const data = JSON.parse(json);
    this.opAnnotationDwgCloud.setAnnotationData(data.cloud);
    this.opAnnotationDwgRect.setAnnotationData(data.rect);
    this.opAnnotationDwgCircle.setAnnotationData(data.circle);
    this.opAnnotationDwgLead.setAnnotationData(data.lead);
    this.opAnnotationDwgText.setAnnotationData(data.text);
  }
}

enum AnnotationDwgType {
  /**
   * 云线
   */
  CLOUD = 1,
  /**
   * 矩形
   */
  RECT = 2,
  /**
   * 圆
   */
  CIRCLE = 3,
  /**
   * 引线
   */
  LEAD = 4,
  /**
   * 文字
   */
  TEXT = 5,
}

export { AnnotationDwgManager, AnnotationDwgType };
